const myRequest = require("../../utils/myRequest");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:30.246908,
    longitude:120.210342,
    markers:[],
    customCalloutMarkerIds:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!getApp().globalData.token) {
      wx.reLaunch({
        url: '/pages/login/login',
      })
      return;
    }
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success: (res) => {
        that.data.latitude =res.latitude
        that.data.longitude =res.longitude
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        that.requestCarParkingList()
      },
      fail: (res) => {
        console.log('定位错误:'+res);
        that.requestCarParkingList()
      },
    })
  },
  /**
   * 获取停车场列表数据
   */
  requestCarParkingList: function () {
    let paramet ={"longitude":this.data.longitude,
                  "latitude":this.data.latitude};
    let that = this;
    myRequest.getRequestData("api/nearbyCarPar", paramet, (isSuccess, res) => {
      if (isSuccess) {
        let markers =[];
        let markerIds =[];
        res.data.forEach(function(item, index) {
          let iconPath ='';
          if (item['type'] === 2) {
            iconPath ='./res_img/map_address_cyan@2x.png'
          } else if (item['nature'] === 0) {
            markerIds.push(item['id'])
            iconPath ='./res_img/map_address_yellow@2x.png'
          } else if (item['nature'] === 1) {
            iconPath ='./res_img/map_address_red@2x.png'
          } else if (item['nature'] === 2) {
            iconPath ='./res_img/map_address_green@2x.png'
          } else if (item['nature'] === 3) {
            iconPath ='./res_img/map_address_blue@2x.png'
          }
          markers.push({
            id: item['id'],
            clusterId: item['address'],
            latitude: item['latitude'],
            longitude: item['longitude'],
            title: item['name'],
            iconPath: iconPath,
            width: '40rpx',
            height: '54rpx',
            customCallout: {
              anchorX: 0,
              anchorY: -5,
              display:'BYCLICK',
            },
          })
        })
        that.setData({
          markers,
          customCalloutMarkerIds: markerIds,
        })
      } else {
        if (res.statusCode === 401) {
          wx.showToast({
            title: "登录已失效! 请重新登录",
            icon: "none",
          })
          wx.reLaunch({
            url: '/pages/login/login',
          });
        } else if (res.data) {
          wx.showToast({
            title: res.data['desc'],
          })
        } else {
          wx.showToast({
            title: "请求失败! 请稍后再试",
            icon: "none",
          })
        }
      }
    })
  },

  markerTap: function() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mapCtx = wx.createMapContext('mapId', this);
    this.mapCtx.setCenterOffset({
      offset: [0.5, 0.4]
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})